//
//  CaculateDate.m
//  Qihuo
//
//  Created by Vencoo on 13-9-29.
//  Copyright (c) 2013年 Vencoo. All rights reserved.
//

#import "CaculateDate.h"
#import "NSDate+Calendar.h"

@implementation CaculateDate

- (BOOL)LeapMonth:(NSDate*)date
{
    if (date.year%400 == 0)
    {
        return YES;
    }
    if (date.year%100 == 0 && date.year%400 != 0)
    {
        return NO;
    }
    if (date.year%100 !=0 && date.year%4 == 0) {
        return YES;
    }
    return YES;
}

- (int)monthDays:(NSDate*)date
{
    if (date.month == 1||date.month == 3||date.month == 5||date.month == 7||date.month == 8||date.month == 10||date.month == 12) {
        return 31;
    }
    if (date.month == 4||date.month == 6||date.month == 9||date.month == 11) {
        return 30;
    }
    if (date.month == 2 && [self LeapMonth:date]) {
        return 29;
    }else{
        return 28;
    }
    return 0;
}
- (int)weekNumber:(NSDate*)date
{
    if (([self monthDays:date]+[self weekdayOfFirstDay:date]-1)%7 == 0) {
        return ([self monthDays:date]+[self weekdayOfFirstDay:date]-1)/7;
        
    }else{
        return ([self monthDays:date]+[self weekdayOfFirstDay:date]-1)/7+1;
        
    }

}
- (NSArray*)dayInMonth:(NSDate*)date
{
    NSMutableArray *days = [NSMutableArray array];
    for (int i = 0; i<[self monthDays:date]; i++) {
        [days addObject:[NSString stringWithFormat:@"%d",i+1]];
    }
    return days;
}
- (int)weekdayOfFirstDay:(NSDate*)date
{
    
   NSDate *firstDay = [NSDate getDateWithString:[NSString stringWithFormat:@"%04d-%02d-01",date.year,date.month] DateFormat:@"yyyy-MM-dd"];
    
    return firstDay.weekday;
}
@end
